<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/pace-js@1/themes/blue/pace-theme-minimal.css">
  <script src="//cdn.jsdelivr.net/npm/pace-js@1/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yimig.github.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="&gt;无扰阅读模式(测试)">
<meta property="og:type" content="article">
<meta property="og:title" content="【C#】事件详解">
<meta property="og:url" content="http://yimig.github.io/2018/11/27/csharp_event/index.html">
<meta property="og:site_name" content="Tim&#39;s Blog">
<meta property="og:description" content="&gt;无扰阅读模式(测试)">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-14_17-15-02-1024x202.png">
<meta property="og:image" content="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-14_17-14-49.png">
<meta property="og:image" content="http://47.94.231.166/wp-content/uploads/2018/11/@event_2018-11-19_23-02-56.png">
<meta property="og:image" content="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-26_17-54-44-1024x550.png">
<meta property="article:published_time" content="2018-11-27T09:38:19.000Z">
<meta property="article:modified_time" content="2020-10-28T14:46:48.396Z">
<meta property="article:author" content="Tim">
<meta property="article:tag" content="C#学习笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-14_17-15-02-1024x202.png">

<link rel="canonical" href="http://yimig.github.io/2018/11/27/csharp_event/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【C#】事件详解 | Tim's Blog</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?64126ab668fa908027cba9464a0afec7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Tim's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Learn Everything!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/yimig" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yimig.github.io/2018/11/27/csharp_event/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.png">
      <meta itemprop="name" content="Tim">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tim's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【C#】事件详解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-11-27 17:38:19" itemprop="dateCreated datePublished" datetime="2018-11-27T17:38:19+08:00">2018-11-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-10-28 22:46:48" itemprop="dateModified" datetime="2020-10-28T22:46:48+08:00">2020-10-28</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Coding/" itemprop="url" rel="index"><span itemprop="name">Coding</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Coding/Csharp/" itemprop="url" rel="index"><span itemprop="name">Csharp</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Coding/Csharp/Common/" itemprop="url" rel="index"><span itemprop="name">Common</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              <span>4.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><a target="_blank" rel="noopener" href="http://47.94.231.166/wp-content/uploads/2018/11/Event.html">>无扰阅读模式(测试)</a><br><a id="more"></a></p>
<h1 id="事件"><a href="#事件" class="headerlink" title="事件"></a>事件</h1><p>[Event]</p>
<hr>
<p>发生→相应”的<strong>五个动作</strong>：</p>
<ul>
<li>事件是使对象或类具备<strong>通知能力</strong>的成员</li>
<li><strong>事件功能=功能+参数（可选）</strong></li>
<li>使用：用于对象或类间的动作协调与信息传递（消息推送）</li>
</ul>
<h2 id="①原理"><a href="#①原理" class="headerlink" title="①原理"></a>①原理</h2><h4 id="事件模型中的两个“5”："><a href="#事件模型中的两个“5”：" class="headerlink" title="事件模型中的两个“5”："></a>事件模型中的两个“5”：</h4><h4 id="“发生→相应”-中的五个部分"><a href="#“发生→相应”-中的五个部分" class="headerlink" title="“发生→相应” 中的五个部分:"></a>“发生→相应” 中的五个部分:</h4><blockquote>
<h4 id="“闹钟1-响了2-我3-起床4”"><a href="#“闹钟1-响了2-我3-起床4”" class="headerlink" title="“闹钟1 响了2 我3 起床4”"></a>“闹钟1 响了2 我3 起床4”</h4><p>1发生事件的对象 2事件的内容 3对事件做出响应的对象 4对事件做出响应的对象的响应内容</p>
<h4 id="5订阅关系"><a href="#5订阅关系" class="headerlink" title="5订阅关系"></a>5订阅关系</h4></blockquote>
<h4 id="“发生→相应”的五个动作："><a href="#“发生→相应”的五个动作：" class="headerlink" title="“发生→相应”的五个动作："></a>“发生→相应”的五个动作：</h4><blockquote>
<h4 id="⑴我有一个事件→⑵一个人或一群人关心我的这个事件→⑶事件发生了→⑷关心这个事件的人被依次通知→⑸被通知的人根据拿到的事件参数对事件进行响应"><a href="#⑴我有一个事件→⑵一个人或一群人关心我的这个事件→⑶事件发生了→⑷关心这个事件的人被依次通知→⑸被通知的人根据拿到的事件参数对事件进行响应" class="headerlink" title="⑴我有一个事件→⑵一个人或一群人关心我的这个事件→⑶事件发生了→⑷关心这个事件的人被依次通知→⑸被通知的人根据拿到的事件参数对事件进行响应"></a>⑴我有一个事件→⑵一个人或一群人关心我的这个事件→⑶事件发生了→⑷关心这个事件的人被依次通知→⑸被通知的人根据拿到的事件参数对事件进行响应</h4></blockquote>
<p>提示： 1. 事件多用于桌面、手机等开发客户端的编程，因为这些程序经常是用户通过事件来“驱动”的。 1. 各编程语言对这个机制实现的方法不尽相同，Java语言无事件成员也无委托这种数据类型，其“事件”是使用<strong>接口</strong>实现的。 1. MVC、MVP、MVVM等模式，是事件模式更高级、更有效的玩法。 1. 日常开发的时候，使用已有事件的机会比较多，自己声明事件的机会比较少。 ## ②事件的应用(使用事件)</p>
<p>功能</p>
<p>实现形式</p>
<p>事件的拥有者</p>
<p>对象</p>
<p>事件成员</p>
<p>成员</p>
<p>事件响应者</p>
<p>对象</p>
<p>事件处理器</p>
<p>成员</p>
<p>事件订阅</p>
<p>(委托)*</p>
<p>*把事件处理器与事件关联在一起，本质上是一种委托类型为基础的约定</p>
<h3 id="示例："><a href="#示例：" class="headerlink" title="示例："></a>示例：</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        Timer timer=<span class="keyword">new</span> Timer();<span class="comment">//定义一个事件的拥有着</span></span><br><span class="line">        timer.Interval=<span class="number">1000</span>;<span class="comment">//设置时钟每1s触发一次Elapsed事件</span></span><br><span class="line">        Boy boy=<span class="keyword">new</span> Boy();<span class="comment">//定义一个事件的响应者</span></span><br><span class="line">        timer.Elapsed+=boy.Action;<span class="comment">//用+=订阅事件</span></span><br><span class="line">        <span class="comment">//↑事件          ↑     ↑事件的处理器</span></span><br><span class="line">        <span class="comment">//               ┕事件的响应者</span></span><br><span class="line">        timer.Start();<span class="comment">//启用时钟</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title">Boy</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">internal</span> <span class="keyword">void</span> <span class="title">Action</span>(<span class="params"><span class="built_in">object</span> sender,ElapsedEventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        Console.Writeline(<span class="string">&quot;Jump!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//事件处理器，建议在订阅事件中自动生成（Ctrl+.+Enter），否则不易确定参数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="两种挂接方式"><a href="#两种挂接方式" class="headerlink" title="两种挂接方式"></a>两种挂接方式</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.button.Click+=<span class="keyword">new</span> EventHandler(<span class="keyword">this</span>.ButtonClicked);</span><br><span class="line"><span class="keyword">this</span>.button.Click+=<span class="keyword">this</span>.ButtonClicked;</span><br></pre></td></tr></table></figure>
<h2 id="③事件的声明（自定义事件）"><a href="#③事件的声明（自定义事件）" class="headerlink" title="③事件的声明（自定义事件）"></a>③事件的声明（自定义事件）</h2><h3 id="四种事件构建形式："><a href="#四种事件构建形式：" class="headerlink" title="四种事件构建形式："></a>四种事件构建形式：</h3><p><a target="_blank" rel="noopener" href="http://upane.cn/archives/962/chrome_2018-11-14_17-15-02"><img src="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-14_17-15-02-1024x202.png" alt=""></a> <a target="_blank" rel="noopener" href="http://upane.cn/archives/962/chrome_2018-11-14_17-14-49"><img src="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-14_17-14-49.png" alt=""></a></p>
<h3 id="示例（使用简化版，注释掉的为标准版）："><a href="#示例（使用简化版，注释掉的为标准版）：" class="headerlink" title="示例（使用简化版，注释掉的为标准版）："></a>示例（使用简化版，注释掉的为标准版）：</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Threading;</span><br><span class="line"></span><br><span class="line">namespace @event</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*这是一个模拟顾客进店点餐的代码：</span></span><br><span class="line"><span class="comment">         代码功能按照以下顺序实现：</span></span><br><span class="line"><span class="comment">         顾客进店，坐下，思考，然后呼叫服务员点餐，服务员记账，用餐完毕付账。  */</span></span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            Customer customer = <span class="keyword">new</span> Customer();<span class="comment">//实例化事件的拥有着：顾客</span></span><br><span class="line">            Waiter waiter = <span class="keyword">new</span> Waiter();<span class="comment">//实例化事件的订阅者：服务员</span></span><br><span class="line">            customer.Order += waiter.Action;<span class="comment">//绑定事件的处理器（订阅事件）</span></span><br><span class="line">            customer.Action();<span class="comment">//顾客：开始行动</span></span><br><span class="line">            customer.PayTheBill();<span class="comment">//顾客：付款</span></span><br><span class="line">            Console.ReadLine();<span class="comment">//暂停程序显示数据</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*声明一个订单（点餐事件的信息），包含菜名和分量两个信息*/</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">class</span> <span class="title">OrderEventArgs</span> : <span class="title">EventArgs</span><span class="comment">//并不强制，但规定凡事件的信息需继承自EventArgs类</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">public</span> <span class="built_in">string</span> DishName &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;<span class="comment">//菜名</span></span><br><span class="line">        <span class="keyword">public</span> <span class="built_in">string</span> Size &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;<span class="comment">//分量</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*声明订餐的委托类，注意这是个类，不要声明在其他类中*/</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">delegate</span> <span class="keyword">void</span> <span class="title">OrderEventHander</span>(<span class="params">Customer customer, OrderEventArgs e</span>)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*声明顾客类，其包含以下信息</span></span><br><span class="line"><span class="comment">     订餐事件、走进餐馆、坐下、思考、呼叫服务员点餐的方法，并有付账方法以及一个记录款项总额的字段用于记录服务员提供的订单*/</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Customer</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">private</span> OrderEventHander orderEventHander;<span class="comment">//声明委托成员，用于记录收听订餐事件的方法</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">/*  //这里是标准的事件定义方法</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        public event OrderEventHander Order//声明点餐事件（事件成员）</span></span><br><span class="line"><span class="comment">        &#123;</span></span><br><span class="line"><span class="comment">            add//添加收听该事件的方法</span></span><br><span class="line"><span class="comment">            &#123;</span></span><br><span class="line"><span class="comment">                this.orderEventHander += value;</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            remove//移除收听该事件的方法</span></span><br><span class="line"><span class="comment">            &#123;</span></span><br><span class="line"><span class="comment">                this.orderEventHander -= value;</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">            //这里很像属性</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">event</span> OrderEventHander Order;<span class="comment">//语法糖</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="built_in">double</span> Bill &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;<span class="comment">//声明一个用于记录服务员回传款项的字段</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">PayTheBill</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            Console.WriteLine(<span class="string">&quot;Customer：I will pay $&#123;0&#125;.&quot;</span>, <span class="keyword">this</span>.Bill);</span><br><span class="line">        &#125;<span class="comment">//顾客埋单</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Walkin</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            Console.WriteLine(<span class="string">&quot;Customer：Walk in the restaurant.&quot;</span>);</span><br><span class="line">        &#125;<span class="comment">//顾客走进餐馆</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">SetDown</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            Console.WriteLine();</span><br><span class="line">        &#125;<span class="comment">//顾客坐下</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Think</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++)</span><br><span class="line">            &#123;</span><br><span class="line">                Console.WriteLine(<span class="string">&quot;Customer：Let me think...&quot;</span>);</span><br><span class="line">                Thread.Sleep(<span class="number">1000</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//顾客想了5秒</span></span><br><span class="line">             <span class="comment">/* //标准的事件调用方法</span></span><br><span class="line"><span class="comment">                 if (this.orderEventHander != null)//排除无方法收听的情况</span></span><br><span class="line"><span class="comment">                 &#123;</span></span><br><span class="line"><span class="comment">                     OrderEventArgs e = new OrderEventArgs();//实例化一个订单信息</span></span><br><span class="line"><span class="comment">                     e.DishName = &quot;Kongpao Chicken&quot;;//顾客点了宫保鸡丁</span></span><br><span class="line"><span class="comment">                     e.Size = &quot;large&quot;;//大份宫保鸡丁</span></span><br><span class="line"><span class="comment">                     this.orderEventHander.Invoke(this, e);//呼叫服务员（执行订餐委托）</span></span><br><span class="line"><span class="comment">                     */</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.Order != <span class="literal">null</span>)<span class="comment">//排除无方法收听的情况</span></span><br><span class="line">            <span class="comment">//这里的调用order事件实为语法糖，实际应该是一个委托</span></span><br><span class="line">            &#123;</span><br><span class="line">                OrderEventArgs e = <span class="keyword">new</span> OrderEventArgs();<span class="comment">//实例化一个订单信息</span></span><br><span class="line">                e.DishName = <span class="string">&quot;Kongpao Chicken&quot;</span>;<span class="comment">//顾客点了宫保鸡丁</span></span><br><span class="line">                e.Size = <span class="string">&quot;large&quot;</span>;<span class="comment">//大份宫保鸡丁</span></span><br><span class="line">                <span class="keyword">this</span>.Order.Invoke(<span class="keyword">this</span>, e);<span class="comment">//呼叫服务员（执行订餐委托）</span></span><br><span class="line">                <span class="comment">//这里的Order也是语法糖</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="comment">//顾客思考并下单</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Action</span>(<span class="params"></span>)<span class="comment">//顾客进店以来点餐前的行动</span></span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            <span class="keyword">this</span>.Walkin();</span><br><span class="line">            <span class="keyword">this</span>.SetDown();</span><br><span class="line">            <span class="keyword">this</span>.Think();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*声明一个服务员类，包含以下信息：</span></span><br><span class="line"><span class="comment">     服务员收听到顾客的召唤（执行订餐委托）所执行的方法*/</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Waiter</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Action</span>(<span class="params">Customer customer, OrderEventArgs e</span>)<span class="comment">//收听订餐委托的方法，接受顾客身份和一道菜的订单</span></span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            Console.WriteLine(<span class="string">&quot;Waiter：I will serve you the dish -&#123;0&#125;&quot;</span>, e.DishName);<span class="comment">//为顾客报菜名</span></span><br><span class="line">            <span class="built_in">double</span> price = <span class="number">10</span>;<span class="comment">//设置基准价格</span></span><br><span class="line">            <span class="keyword">switch</span> (e.Size)<span class="comment">//大份菜是基准价格的1.5倍，小份菜是基准价格的0.5倍</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;large&quot;</span>:</span><br><span class="line">                    price *= <span class="number">1.5</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;small&quot;</span>:</span><br><span class="line">                    price *= <span class="number">0.5</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="literal">default</span>: <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            customer.Bill += price;<span class="comment">//为顾客的账单添加一道菜的款项</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 事件的拥有者：Customer</span></span><br><span class="line"><span class="comment">     * 事件成员：Order</span></span><br><span class="line"><span class="comment">     * 事件的响应者：Waiter</span></span><br><span class="line"><span class="comment">     * 事件处理器：wait.Action(Customer customer, OrderEventArgs e)</span></span><br><span class="line"><span class="comment">     * 事件订阅：customer.Order += waiter.Action;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果： <a target="_blank" rel="noopener" href="http://upane.cn/archives/946/event_2018-11-19_23-02-56"><img src="http://47.94.231.166/wp-content/uploads/2018/11/@event_2018-11-19_23-02-56.png" alt=""></a></p>
<h3 id="归纳总结："><a href="#归纳总结：" class="headerlink" title="归纳总结："></a>归纳总结：</h3><ul>
<li>事件<code>event</code>与委托类型字段的区别：语义化，防止滥用</li>
<li>事件的本质：<ol>
<li>是委托字段的一个包装器</li>
<li>该包装器对委托字段的访问起限制作用，相当于一个蒙版</li>
<li>封装的一个重要的功能就是隐藏</li>
<li>事件对外界隐藏了委托实例的大部分功能，仅暴露添加/移除事件访问器的功能</li>
</ol>
</li>
<li><p><code>EventHandle</code>委托：.Net定义好的常用事件：</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="built_in">delegate</span> <span class="keyword">void</span> <span class="title">EventHandler</span>(<span class="params"><span class="built_in">object</span> sender,EventArgs e</span>)</span></span><br><span class="line"><span class="function"><span class="comment">//              ↑无返回值            ↑两个最基本的类型↑</span></span></span><br></pre></td></tr></table></figure>
<p>使用时需进行类型转换：</p>
<blockquote>
<p>例：xxxEventArgs args=e as xxxEventArgs //e原为EventArgs类型</p>
</blockquote>
</li>
<li><p>用于声明事件的委托类型的命名约定：</p>
<ul>
<li>用于声明Foo事件的委托，一般命名为FooEventHandler</li>
<li>FooEventHandler委托的参数一般有两个（由win32演化而来）</li>
<li>第一个是<code>object</code>类型，名字为sender，实际上是<strong>事件的拥有者</strong>，事件的source</li>
<li>第二个是<code>EventArgs</code>类的派生类，类名一般为FooEventArgs，参数为e，也就是<strong>事件参数</strong></li>
<li>虽无官方说法，但我们可以把委托的参数列表看作是事件发生以后发送给事件响应者的“事件消息”</li>
<li>触发Foo事件的方法一般命名为OnFoo即“因何而发”、<strong>“事出有因”</strong></li>
<li>访问级别为<code>protected</code>,不应为<code>public</code></li>
</ul>
</li>
<li>事件的命名约定：<ul>
<li>带有时态动词或动词短语</li>
<li>事件拥有者“正在做”什么事，用<strong>进行时</strong>，事件拥有者“做完了”什么事，用<strong>完成时</strong>。</li>
</ul>
</li>
</ul>
<h2 id="④后记："><a href="#④后记：" class="headerlink" title="④后记："></a>④后记：</h2><p><a target="_blank" rel="noopener" href="http://upane.cn/archives/962/chrome_2018-11-26_17-54-44"><img src="http://47.94.231.166/wp-content/uploads/2018/11/chrome_2018-11-26_17-54-44-1024x550.png" alt=""></a></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/C-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"># C#学习笔记</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/11/19/csharp_traditional_event/" rel="prev" title="【C#】事件的传统定义方法">
      <i class="fa fa-chevron-left"></i> 【C#】事件的传统定义方法
    </a></div>
      <div class="post-nav-item">
    <a href="/2018/11/27/coding_graph/" rel="next" title="【数据结构】图的创建与遍历">
      【数据结构】图的创建与遍历 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">事件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E2%91%A0%E5%8E%9F%E7%90%86"><span class="nav-number">1.1.</span> <span class="nav-text">①原理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6%E6%A8%A1%E5%9E%8B%E4%B8%AD%E7%9A%84%E4%B8%A4%E4%B8%AA%E2%80%9C5%E2%80%9D%EF%BC%9A"><span class="nav-number">1.1.0.1.</span> <span class="nav-text">事件模型中的两个“5”：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%80%9C%E5%8F%91%E7%94%9F%E2%86%92%E7%9B%B8%E5%BA%94%E2%80%9D-%E4%B8%AD%E7%9A%84%E4%BA%94%E4%B8%AA%E9%83%A8%E5%88%86"><span class="nav-number">1.1.0.2.</span> <span class="nav-text">“发生→相应” 中的五个部分:</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%80%9C%E9%97%B9%E9%92%9F1-%E5%93%8D%E4%BA%862-%E6%88%913-%E8%B5%B7%E5%BA%8A4%E2%80%9D"><span class="nav-number">1.1.0.3.</span> <span class="nav-text">“闹钟1 响了2 我3 起床4”</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5%E8%AE%A2%E9%98%85%E5%85%B3%E7%B3%BB"><span class="nav-number">1.1.0.4.</span> <span class="nav-text">5订阅关系</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%80%9C%E5%8F%91%E7%94%9F%E2%86%92%E7%9B%B8%E5%BA%94%E2%80%9D%E7%9A%84%E4%BA%94%E4%B8%AA%E5%8A%A8%E4%BD%9C%EF%BC%9A"><span class="nav-number">1.1.0.5.</span> <span class="nav-text">“发生→相应”的五个动作：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%B4%E6%88%91%E6%9C%89%E4%B8%80%E4%B8%AA%E4%BA%8B%E4%BB%B6%E2%86%92%E2%91%B5%E4%B8%80%E4%B8%AA%E4%BA%BA%E6%88%96%E4%B8%80%E7%BE%A4%E4%BA%BA%E5%85%B3%E5%BF%83%E6%88%91%E7%9A%84%E8%BF%99%E4%B8%AA%E4%BA%8B%E4%BB%B6%E2%86%92%E2%91%B6%E4%BA%8B%E4%BB%B6%E5%8F%91%E7%94%9F%E4%BA%86%E2%86%92%E2%91%B7%E5%85%B3%E5%BF%83%E8%BF%99%E4%B8%AA%E4%BA%8B%E4%BB%B6%E7%9A%84%E4%BA%BA%E8%A2%AB%E4%BE%9D%E6%AC%A1%E9%80%9A%E7%9F%A5%E2%86%92%E2%91%B8%E8%A2%AB%E9%80%9A%E7%9F%A5%E7%9A%84%E4%BA%BA%E6%A0%B9%E6%8D%AE%E6%8B%BF%E5%88%B0%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%8F%82%E6%95%B0%E5%AF%B9%E4%BA%8B%E4%BB%B6%E8%BF%9B%E8%A1%8C%E5%93%8D%E5%BA%94"><span class="nav-number">1.1.0.6.</span> <span class="nav-text">⑴我有一个事件→⑵一个人或一群人关心我的这个事件→⑶事件发生了→⑷关心这个事件的人被依次通知→⑸被通知的人根据拿到的事件参数对事件进行响应</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9A"><span class="nav-number">1.1.1.</span> <span class="nav-text">示例：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%A4%E7%A7%8D%E6%8C%82%E6%8E%A5%E6%96%B9%E5%BC%8F"><span class="nav-number">1.1.2.</span> <span class="nav-text">两种挂接方式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E2%91%A2%E4%BA%8B%E4%BB%B6%E7%9A%84%E5%A3%B0%E6%98%8E%EF%BC%88%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BA%8B%E4%BB%B6%EF%BC%89"><span class="nav-number">1.2.</span> <span class="nav-text">③事件的声明（自定义事件）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%9B%E7%A7%8D%E4%BA%8B%E4%BB%B6%E6%9E%84%E5%BB%BA%E5%BD%A2%E5%BC%8F%EF%BC%9A"><span class="nav-number">1.2.1.</span> <span class="nav-text">四种事件构建形式：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%88%E4%BD%BF%E7%94%A8%E7%AE%80%E5%8C%96%E7%89%88%EF%BC%8C%E6%B3%A8%E9%87%8A%E6%8E%89%E7%9A%84%E4%B8%BA%E6%A0%87%E5%87%86%E7%89%88%EF%BC%89%EF%BC%9A"><span class="nav-number">1.2.2.</span> <span class="nav-text">示例（使用简化版，注释掉的为标准版）：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BD%92%E7%BA%B3%E6%80%BB%E7%BB%93%EF%BC%9A"><span class="nav-number">1.2.3.</span> <span class="nav-text">归纳总结：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E2%91%A3%E5%90%8E%E8%AE%B0%EF%BC%9A"><span class="nav-number">1.3.</span> <span class="nav-text">④后记：</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Tim"
      src="/uploads/avatar.png">
  <p class="site-author-name" itemprop="name">Tim</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">90</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">40</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yimig" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yimig" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:zhangyiming@programmer.net" title="E-Mail → mailto:zhangyiming@programmer.net" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://leetcode.com/yimig/" title="LeeCode → https:&#x2F;&#x2F;leetcode.com&#x2F;yimig&#x2F;" rel="noopener" target="_blank"><i class="fa fa-fw fa-code"></i>LeeCode</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/tim-2-96" title="zhihu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;tim-2-96" rel="noopener" target="_blank"><i class="fa fa-fw fa-question-circle"></i>zhihu</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://upane.cn/" title="https:&#x2F;&#x2F;upane.cn" rel="noopener" target="_blank">博客旧址</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tim</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">183k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:46</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>


  <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/three.min.js"></script>
    <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/canvas_sphere.min.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : 'a876807f33858291f9d9',
      clientSecret: '7a5c6a83611c309965d7339d7ca5f16e34a21e9f',
      repo        : 'yimig.github.io',
      owner       : 'yimig',
      admin       : ['yimig'],
      id          : '420fd26171714af72073e4c614d052f9',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>
